<?php

require_once ROOT.'/app/task.php';

class bookshelf extends page{
	
	function add(){
		$uid = $this->req['uid'];
		$bookid = $this->req['bid'];
		$bookmark = $this->req['mark'] ? $this->req['mark'] : '';
		
		$json = array();
		if(!$uid || !$bookid){
			$json['status'] = $this->f_status;
			$json['message'] = '缺少字段';
			G::toClient($json);
		}
		
		$select_sql = "select * from bookshelf where uid='{$uid}' limit 1";
		$select_rs=$this->db->query($select_sql)->fetch();
		if(!$select_rs){
			$bookList = array(
				$bookid => $bookmark,
			);
			$books = json_encode($bookList);
			
			$insert_sql="insert into bookshelf (uid, books, create_time) values ('{$uid}', '{$books}', now())";
			$return = $this->db->exec($insert_sql);
		}else{
			$bookList = json_decode($select_sql['books'], true);
			$bookList[$bookid] = $bookmark;
			$books = json_encode($bookList);
			
			$update_sql = "update bookshelf set books = '{$books}' where uid='{$uid}' limit 1";
			$return = $this->db->exec($update_sql);
		}
		
		if(!$return){
			$json['status'] = $this->f_status;
		}else{
			$json['status'] = $this->s_status;
		}
		G::toClient($json);
	}
	
	function det(){
		$uid = $this->req['uid'];
		$bookid = $this->req['bid'];
		
		$json = array();
		if(!$uid || !$bookid){
			$json['status'] = $this->f_status;
			$json['message'] = '缺少字段';
			G::toClient($json);
		}
		
		$select_sql = "select * from bookshelf where uid='{$uid}' limit 1";
		$select_rs=$this->db->query($select_sql)->fetch();
		if(!$select_rs){
			$json['status'] = $this->f_status;
			$json['message'] = '无效用户';
			G::toClient($json);
		}
		
		$books = $select_rs['books'];
		$bookList = json_decode($books, true);
		unset($bookList[$bookid]);
		$books = json_encode($bookList);
		
		$update_sql = "update bookshelf set books = '{$books}' where uid='{$uid}' limit 1";
		$return = $this->db->exec($update_sql);
		
		if(!$return){
			$json['status'] = $this->f_status;
		}else{
			$json['status'] = $this->s_status;
		}
		G::toClient($json);
	}
	
	function bookList(){
		$uid = $this->req['uid'];
		
		$json = array();
		if(!$uid){
			$json['status'] = $this->f_status;
			$json['message'] = '缺失uid';
			G::toClient($json);
		}
		
		$select_sql = "select * from bookshelf where uid='{$uid}' limit 1";
		$select_rs=$this->db->query($select_sql)->fetch();
		if(!$select_rs){
			$json['status'] = $this->f_status;
			$json['message'] = '无效用户';
			G::toClient($json);
		}
		
		$json['list'] = json_decode($select_rs['books'], true);
		$json['status'] = $this->s_status;
		G::toClient($json);
	}
	
	function bookupdate(){
		$uid = $this->req['uid'];
		$bookids = $this->req['bids'];
		
		$json = array();
		if(!$uid || !$bookids){
			$json['status'] = $this->f_status;
			$json['message'] = '缺失字段';
			G::toClient($json);
		}
		
		$sql = "select id, last from book where id in ({$bookids})";		
		$rs = $this->db->query($sql)->fetchAll();
		if($rs){
			$bookidList = explode(',', $bookids);
			foreach($bookidList as $id){
				$taskClass = new task();
				$taskClass->create($id, 2);
			}
		}
		
		$json['list'] = $rs;
		$json['status'] = $this->s_status;
		G::toClient($json);
	}
	
}
